# -*- coding: utf-8 -*-
from osv import fields,osv
from openerp.tools.translate import _
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DATE_FORMAT
from datetime import datetime

class pm_customer_contract_end(osv.osv_memory):
    _name = 'pm.customer.contract.end'

    def default_get(self,cr,uid,fields,context=None):
        contract_obj = self.pool.get('pm.customer.contract')
        contract_ids = context.get('active_ids',[])
        print 'contract_ids is %s' % contract_ids
        res = super(pm_customer_contract_end,self).default_get(cr,uid,fields,context=context)
        contract_item = contract_obj.read(cr,uid,contract_ids[0],['start_date','end_date','partner_id'],context=context)
        print 'contract_item is %s' % contract_item
        res['contract_id'] = contract_ids[0]
        res['source_start_date'] = contract_item['start_date']
        res['source_end_date'] = contract_item['end_date']
        res['partner_id'] = contract_item['partner_id'][0]
        return res

    def end(self,cr,uid,ids,context=None):
        end_item = self.read(cr,uid,ids[0],context=context)
        contract_rep = self.pool.get('pm.customer.contract')
        contract_item = contract_rep.read(cr,uid,end_item['contract_id'][0],[],context=context)
        print 'contract_item is %s' % contract_item

        source_end_date = contract_item['end_date']
        new_end_date = end_item['new_end_date']
        if datetime.strptime(source_end_date,DATE_FORMAT)<datetime.strptime(new_end_date,DATE_FORMAT):
            raise osv.except_osv(_('不能终止'),_('合同的终止日期必须在原合同起租和止租日期之间！'))

        room_id = contract_item['room_id'][0]
        out_item = {
            'project_id':contract_item['project_id'][0],
            'building_id':contract_item['building_id'][0],
            'area_id':contract_item['area_id'][0],
            'room_id':contract_item['room_id'][0],
            'partner_id':contract_item['partner_id'][0],
            'receive_object':end_item['receive_object'],
            'checkout_date':new_end_date,
            'hourse_state':end_item['hourse_state'] and end_item['hourse_state'][0] or None,
            'auto_clear_leftamout':end_item['auto_clear_leftamout'],
            'remark':u'合同自动迁出',
        }
        self.pool.get('pm.customer.checkout').create(cr,uid,out_item,context=context)
        contract_values = {'end_date':new_end_date,'state':'end','remark':u'手动终止合同'}
        return contract_rep.write(cr,uid,contract_item['id'],contract_values,context=context)

    _columns = {
        'contract_id':fields.many2one('pm.customer.contract',string='租赁合同'),
        'partner_id':fields.related('contract_id',type='many2one',relation='res.partner',string='客户名称'),
        'source_start_date':fields.related('contract_id','start_date',type='date',string='原起租日期'),
        'source_end_date':fields.related('contract_id','end_date',type='date',string='原止租日期'),
        'new_end_date':fields.date(string='终止日期',required=True),
        'receive_object':fields.selection([('owner','业主'),('no','无')],string='迁出后收费服务对象'),
        'hourse_state':fields.many2one('pm.parameter',string='迁出房间后状态',domain="[('category_code','=','FJZT')]"),
        'auto_clear_leftamout':fields.boolean(string='自动结清余额'),
    }
pm_customer_contract_end()


